package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.glowmee.cmd.RegisterCMD;
import com.glowdayz.glowmee.dao.CommonCodeDAO;
import com.glowdayz.glowmee.dao.RegisterDAO;

public class ListRegisterBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public ListRegisterBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		String where = request.getParameter("where");
		if (where != null) {
			where = Util.getInstance().incoding(where);
		}
		
		String id_str = request.getParameter("id");
		String isActivity_str = request.getParameter("isActivity");
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		if (order != null && order.equals("isUnActivity")) {
			order = "isActivity ASC, create_date DESC";
			request.setAttribute("order", "&order=isUnActivity");
		} else if (order != null && order.equals("isActivity")) {
			order = "isActivity DESC, create_date DESC";
			request.setAttribute("order", "&order=isActivity");
		}
		
		RegisterDAO dao = RegisterDAO.getInstance();
		
		if (id_str != null && isActivity_str != null) {
			int id = Integer.parseInt(id_str);
			int isActivity = Integer.parseInt(isActivity_str);
			
			if (isActivity == 1) {
				isActivity = 0;
			} else if (isActivity == 0) {
				isActivity = 1;
			}
			
			RegisterCMD cmd = new RegisterCMD(id, isActivity);
			try {
				dao.unActivityRegister(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		CommonCodeDAO commonDao = CommonCodeDAO.getInstance();
		try {
			request.setAttribute("skinType", commonDao.getCommonCodeList(null, "parentId=2"));
			request.setAttribute("sido", commonDao.getCommonCodeList(null, "parentId=4"));
			String sql = "SELECT c1.idCommon, c1.parentId, c1.commonText, c1.sortKey, c1.isDisplay, c1.create_date, c1.modified_date "
					+ "FROM commoncode c1, commoncode c2 "
					+ "WHERE c1.parentId=c2.idCommon AND c2.parentId=4";
			request.setAttribute("gugun", commonDao.getCommonCodeList(sql));
			request.setAttribute("birthYear", commonDao.getCommonCodeList("idCommon DESC", "parentId=5"));
		} catch (Exception e1) {
			DBHandler.getInstance().disconnect();
			e1.printStackTrace();
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("register", dao.getRegisterList(page, 10, "USER", order, where));
			if (where != null) {
				request.setAttribute("where", "&where=" + URLEncoder.encode(where, "UTF-8"));
				request.setAttribute("activity", DBHandler.getInstance().getCount(
						"register", "idRegister", "userType='USER' AND isActivity='1'" + where));
				request.setAttribute("unActivity", DBHandler.getInstance().getCount(
						"register", "idRegister", "userType='USER' AND isActivity='0'" + where));
			} else {
				request.setAttribute("activity", DBHandler.getInstance().getCount("register", "idRegister", "userType='USER' AND isActivity='1'"));
				request.setAttribute("unActivity", DBHandler.getInstance().getCount("register", "idRegister", "userType='USER' AND isActivity='0'"));
			}
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/register/listRegister.jsp");
		dpr.forward(request, response);
	}

	/** 검색결과로 처리되는 구간 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		request.setCharacterEncoding("utf-8");
		
		String where = "";
		
		String nickName = request.getParameter("nickName");
		String gender = request.getParameter("gender");
		String birthYear1 = request.getParameter("birthYear1");
		String birthYear2 = request.getParameter("birthYear2");
		String created_date1 = request.getParameter("created_date1");
		String created_date2 = request.getParameter("created_date2");
		String eMail = request.getParameter("eMail");
		String skinType = request.getParameter("skinType");
		String location1 = request.getParameter("location1");
		String location2 = request.getParameter("location2");
		String recommendNickname = request.getParameter("recommendNickname");
		
		if (nickName != null && !nickName.equals("")) {
			where += " AND nickName like '%" + nickName + "%'";
		}
		if (gender != null && !gender.equals("")) {
			where += " AND gender = " + gender;
		}
		if (!birthYear1.equals("--- 시작년도 ---") && !birthYear2.equals("--- 끝년도 ---")) {
			where += " AND birthYear BETWEEN " + birthYear1 + " AND " + birthYear2;
		}
		if (!created_date1.equals("") && !created_date2.equals("")) {
			created_date1 = created_date1.replace("-", "");
			created_date1 += "000000";
			created_date2 += "999999";
			created_date2 = created_date2.replace("-", "");
			where += " AND create_date BETWEEN " + created_date1 + " AND " + created_date2;
		}
		if (eMail != null && !eMail.equals("")) {
			where += " AND eMail like '%" + eMail + "%'";
		}
		if (skinType != null && !skinType.equals("--- 선택하세요 ---")) {
			where += " AND skinType = " + skinType;
		}
		if (location2 != null && !location2.equals("default")) {
			where += " AND location = " + location2;
		} else if (location1 != null && !location1.equals("default")) {
			where += " AND location in (SELECT idCommon FROM commoncode WHERE parentId = " + location1 + ")";
		}
		if (recommendNickname != null && !recommendNickname.equals("")) {
			where += " AND recommender in (SELECT idRegister FROM register WHERE nickName like '%" + recommendNickname + "%'" + ")";
		}
		
		String id_str = request.getParameter("id");
		String isActivity_str = request.getParameter("isActivity");
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		if (order != null && order.equals("isUnActivity")) {
			order = "isActivity ASC, create_date DESC";
			request.setAttribute("order", "&order=isUnActivity");
		} else if (order != null && order.equals("isActivity")) {
			order = "isActivity DESC, create_date DESC";
			request.setAttribute("order", "&order=isActivity");
		}
		
		RegisterDAO dao = RegisterDAO.getInstance();
		
		if (id_str != null && isActivity_str != null) {
			int id = Integer.parseInt(id_str);
			int isActivity = Integer.parseInt(isActivity_str);
			
			if (isActivity == 1) {
				isActivity = 0;
			} else if (isActivity == 0) {
				isActivity = 1;
			}
			
			RegisterCMD cmd = new RegisterCMD(id, isActivity);
			try {
				dao.unActivityRegister(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		CommonCodeDAO commonDao = CommonCodeDAO.getInstance();
		try {
			request.setAttribute("skinType", commonDao.getCommonCodeList(null, "parentId=2"));
			request.setAttribute("sido", commonDao.getCommonCodeList(null, "parentId=4"));
			String sql = "SELECT c1.idCommon, c1.parentId, c1.commonText, c1.sortKey, c1.isDisplay, c1.create_date, c1.modified_date "
					+ "FROM commoncode c1, commoncode c2 "
					+ "WHERE c1.parentId=c2.idCommon AND c2.parentId=4";
			request.setAttribute("gugun", commonDao.getCommonCodeList(sql));
			request.setAttribute("birthYear", commonDao.getCommonCodeList("idCommon DESC", "parentId=5"));
		} catch (Exception e1) {
			DBHandler.getInstance().disconnect();
			e1.printStackTrace();
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("register", dao.getRegisterList(1, 10, "USER", order, where));
			request.setAttribute("where", "&where=" + URLEncoder.encode(where, "UTF-8"));
			request.setAttribute("activity", DBHandler.getInstance().getCount("register", "idRegister", "userType='USER' AND isActivity='1'" + where));
			request.setAttribute("unActivity", DBHandler.getInstance().getCount("register", "idRegister", "userType='USER' AND isActivity='0'" + where));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/register/listRegister.jsp");
		dpr.forward(request, response);
	}

}